package com.lwf.core.processor;

import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.List;
import java.util.Optional;

public abstract class AbstractPageDetailsProcessor implements PageProcessor {


    public void process(Page page) {
        String url = page.getUrl().get();
        Html html = page.getHtml();
        boolean isListPage = getListUrlRegex().stream().anyMatch(item -> {
            return url.matches(item);
        });
        boolean isDetailsPage = getDetailsUrlRegex().stream().anyMatch(item -> {
            return url.matches(item);
        });
        //如果是列表页面
        if (isListPage) {
            List<String> detailsUrlList = getDetailsUrlList(html);
            for (String detailsUrl : detailsUrlList) {
                boolean isDetailsPageTemp = getDetailsUrlRegex().stream().anyMatch(item -> {
                    return detailsUrl.matches(item);
                });
                if (isDetailsPageTemp == true) {
                    page.addTargetRequest(detailsUrl);
                }
            }
            Optional<String> first = getNextPageUrl(html).stream().filter(item -> !StringUtils.isEmpty(item)).findFirst();
            //String nextPageUrl = getNextPageUrl(html);
            if (first.isPresent()) {
                page.addTargetRequest(first.get());
            }
            page.setSkip(true);
        } else if (isDetailsPage) {
            String title = null;
            Optional<String> first = getTitle(html).stream().filter(item -> !StringUtils.isEmpty(item)).findFirst();
            if (first.isPresent()) {
                title = first.get();
            }
            String htmlStr = getHtmlStr(html);
            page.putField("title", title);
            page.putField("htmlStr", htmlStr);
            page.putField("url", page.getUrl().get());
        }

    }

    /**
     * 列表页url的正则表达式
     *
     * @return
     */
    protected abstract List<String> getListUrlRegex();

    /**
     * 获取详情页url的正则表达式
     *
     * @return
     */
    protected abstract List<String> getDetailsUrlRegex();

    /**
     * 获取详情页url
     *
     * @param html
     * @return
     */
    protected abstract List<String> getDetailsUrlList(Html html);

    /**
     * 获取下一页url
     *
     * @param html
     * @return
     */
    protected abstract List<String> getNextPageUrl(Html html);

    /**
     * 获取文章标题
     *
     * @param html
     * @return
     */
    protected abstract List<String> getTitle(Html html);

    /**
     * 获取详情页的html源码
     *
     * @param html
     * @return
     */
    protected abstract String getHtmlStr(Html html);


}
